package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    /**
     * 1.如果用户是新增 则将密码加密处理之后新增操作
     * 2.如果用户是修改
     *              改密码   加密处理
     *              不改密码  直接修改.
     * @param systemUser
     */
    @Override
    public void saveOrUpdateItem(SystemUser systemUser) {
        //根据密码判断 如果有密码则加密处理
        if(StringUtils.hasLength(systemUser.getPassword())){
            //采用md5Hash的方式加密
            String password = DigestUtils.sha256Hex(systemUser.getPassword().getBytes());
            systemUser.setPassword(password);
        }

        //如果是新增 默认用户开启状态
        if(systemUser.getId() == null){
            systemUser.setStatus(BaseStatus.ENABLE);
        }
        this.saveOrUpdate(systemUser);
    }

    @Override
    public IPage<SystemUserItemVo> pageItem(IPage<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
        return systemUserMapper.pageItem(page,queryVo);
    }

    @Override
    public SystemUserItemVo selectUserById(Long id) {

        return systemUserMapper.selectUserById(id);
    }
}




